var beiMiCommon = require("BeiMiCommon");
cc.Class({
    extends: beiMiCommon,

    properties: {
        createRoomType:{
            default:null,
            type:cc.Prefab
          },
    },
    onLoad:function(){
        if(cc.beimi.roomType==2){
            this.num = 8;
        }
        else{
            this.num = 5;
        }
    },
    onCreateRoomTypeClick:function(){
        cc.beimi.openwin2 = cc.instantiate(this.createRoomType) ;
        cc.beimi.openwin2.parent = cc.find('Canvas');
    },
    //选择局数
    selectNum:function(event,data){
        //console.log(data)
        var node = cc.find("Canvas/createroom");
        var ticket = node.getChildByName("base1").getChildByName("zhifu").getChildByName("label").getComponent(cc.Label);
        if(data==5 || data==8){
            ticket.string = "房主支付2房卡8"
        }
        else if(data==10 || data==16){
            ticket.string = "房主支付4房卡"
        }
        else if(data==15){
            ticket.string = "房主支付6房卡"
        }
        this.num = data;
    },
    //选择玩法
    selectWay:function(event,data){
        console.log(data)
    },
    //刷新俱乐部列表
    refreshClub:function(){
        let self = this;
        var param = "";
        let socket = this.socket();
        socket.emit("getclubs", param);
        var thisEvent = new cc.Event.EventCustom("getclubs", true);
        self.node.dispatchEvent(thisEvent);
    },
    //创建房间
    createRoom:function(event, type){
        this.loadding();
        let self = this;
        var extparams = {};
        // var extparams = {
        //     gametype : data.code,
        //     playway :  data.id,
        //     gamemodel : "room"
        // };
        var gametype = null;
        var playways = cc.beimi.games[2].types[0].playways;
        if(type==1){
            extparams = {
                gametype : playways[0].code,
                playway :  playways[0].id,
                gamemodel : "room"
            };
            gametype = playways[0].code;
        }
        else{
            extparams = {
                gametype : playways[2].code,
                playway :  playways[2].id,
                gamemodel : "room"
            };
            gametype = playways[2].code;
        }
        // var extparams = {
        //     gametype : "shuangkou",
        //     playway :  "402840816827c208016827c792080099",
        //     gamemodel : "room"
        // };
        //this.preload(extparams , this);
        //let socket = this.socket();
        //cc.beimi.socket.exec("joinroom" , param);
        var param = {
            token:cc.beimi.authorization,
            playway:extparams.playway,
            orgi:cc.beimi.user.orgi,
            extparams:extparams,
            clubid: cc.beimi.clubId,
            numofgames: self.num,
            gametype: gametype
        };
        let socket = this.socket();
        /**
         * 发送创建房间开始游戏的请求
         */
        event.stopPropagation();
        console.log(param);
        socket.exec("createroom" , param);
        cc.beimi.socket.on("createroom" , function(result){
          console.log(JSON.parse(result))
          var res = JSON.parse(result);
          if(res.gameRoom.showid){
            cc.beimi.roomid = res.gameRoom.showid;
            cc.beimi.totalNum = self.num;
            //self.preload(extparams , self);
            setTimeout(function(){
                //刷新
                self.refreshClub();
                self.closeOpenWin();
                self.closeOpenWin2();
                self.closeloadding();
            }, 1500)

            // var newParam = "";
            // socket.emit("getclubs", newParam);
            // var thisEvent = new cc.Event.EventCustom("getclubs", true);
            // self.node.dispatchEvent(thisEvent);
            // var thisEvent = new cc.Event.EventCustom("getrooms", true);
            // self.node.dispatchEvent(thisEvent);
          }
          //self.alert("房间号不存在。");
        });
    }
});
